<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class shopping extends CI_Controller {
	public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('user_is_login')){
			redirect('/User_login/login');
		}
	}

	public function addToCart(){
		$user_id=$this->session->userdata('user_id');
		$product_id=$this->input->post('product_id');
		$num=$this->input->post('num');
		$this->load->model('login_model');
		$data=$this->login_model->get_one_cart($user_id,$product_id);
		if($this->login_model->get_one_cart($user_id,$product_id)){
			$this->login_model->update_cart($user_id,$product_id,$num);
		}else{
			$this->login_model->add_cart($user_id,$product_id,$num);
		}
		redirect('Product/detail/'.$product_id);
	}
	public function shopcart(){
		$user_id=$this->session->userdata('user_id');
		$this->load->model('login_model');
		$this->load->model('Product_model');
		$user_cata=$this->login_model->get_all_cata_id($user_id);
		$data=array(
			'user_cata'=>$user_cata
		);
		$this->load->view('shopcart',$data);		
	}
	public function shopcart_delete($id){
		$this->load->model('login_model');
		$this->login_model->delete_one_cart($id);
		redirect('/shopping/shopcart');
	}
	public function cartdels(){
		$ids=$this->input->post('ids');
		$this->load->model('login_model');
		$this->login_model->cartdels($ids);
		echo 'ok';
	}
	public function update_cart_num(){
		$id=$this->input->get('id');
		$num=$this->input->get('num');
		$this->load->model('login_model');
		if($num>0){
			$this->login_model->update_cart_num($id,$num);
			echo 'ok';
		}else{
			echo '数量不能小于0';
		}
	}
	public function createorder(){
		$prve=$this->input->post('data');
		if(count($prve)==0){
			echo '请选择一件商品';
		}else{
			$this->session->set_userdata('prve',$prve);
			echo 'ok';
		}
	}
	public function shoporder(){
		//var_dump($this->input->post());
		if($this->input->post()){
			$address_id=$this->input->post('address_id');
			$product_id=$this->input->post('product_id');
			$num=$this->input->post('num');
			$this->load->model('order_model');
			$order_id=$this->order_model->add_one_order($this->session->userdata('user_id'),$address_id);
			$count=count($product_id);
			for($i=0;$i<$count;$i++){
				$this->order_model->add_products_to_order($order_id,$product_id[$i],$num[$i]);
			}
		}
		$this->load->model('Product_model');
		$this->load->model('user_address_model');
		$data=array(
			'data'=>$this->session->userdata('prve'),
			'address'=>$this->user_address_model->get_user_all_address($this->session->userdata('user_id'))
		);
		$this->load->view('shopping_createorder', $data);
	}
	public function add_one_ADDress(){
		$name=$this->input->post('name');
		$address=$this->input->post('address');
		$phone=$this->input->post('phone');
		if(empty($name)){
          	echo json_encode(array('code' => 'error','filed' => 'name','msg' => "姓名不能为空"));
          	exit();
	    }
	    if(empty($address)){
          	echo json_encode(array('code' => 'error','filed' => 'address','msg' => "地址不能为空"));
          	exit();
	    }
	    if(empty($phone)){
            echo json_encode(array('code' => 'error','filed' => 'phone','msg' => "电话不能为空"));
          	exit();
	    }
		$this->load->model('user_address_model');
		$this->user_address_model->add_one_address($name,$address,$phone,$this->session->userdata('user_id'));
		echo json_encode(array('code' => 'ok'));
	}
	public function del($id){
		$this->load->model('user_address_model');
		$this->user_address_model->delete($id);
		redirect('/shopping/shoporder');
	}
}
